package com.strelitzia.controller;

import com.strelitzia.entity.ReviewEntity;

import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.FriendsService;
import com.strelitzia.service.ReviewService;
import com.strelitzia.service.UserService;

import com.strelitzia.vo.ResultVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @create 2023/12/26 15:33
 */
@RestController
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    ReviewService reviewService;
    @Autowired
    UserService userService;
    @Autowired
    FriendsService friendsService;

    @GetMapping("/time/{did}")
    public ResultVo getTop3ForTime(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.getTop3ForTime(did));
        } catch (Exception e) {
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/hot/{did}")
    public ResultVo getTop3ForHot(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.getTop3ForHot(did));
        } catch (Exception e) {
            resultVo.setCode(1);
        }
        return resultVo;
    }


    @GetMapping("/time/all/{did}")
    public ResultVo getAllForTime(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.getAllForTime(did));
        } catch (Exception e) {
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/hot/all/{did}")
    public ResultVo getAllForHot(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.getAllForHot(did));
        } catch (Exception e) {
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/{did}/{content}")
    public ResultVo review(@PathVariable("did") Integer did, @PathVariable("content") String content) {
        ResultVo resultVo = new ResultVo();

        try {
            reviewService.review(did,content);
        }  catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/like/{id}")
    public ResultVo like(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            reviewService.like(id);
        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/unlike/{id}")
    public ResultVo unlike(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            reviewService.unlike(id);
        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/del/{id}")
    public void del(@PathVariable("id") Integer id) {
        reviewService.del(id);
    }

    @Transactional
    @GetMapping("/recover/{id}/{context}")
    public ResultVo recover(@PathVariable("id") Integer id, @PathVariable("context") String context) {
        ResultVo resultVo = new ResultVo();
        try {
            reviewService.recover(id, context);
        }  catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/reviews/hot/{id}")
    public ResultVo reviewsForHot(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.reviewsForHot(id));
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }

    @GetMapping("/reviews/time/{id}")
    public ResultVo reviewsForTime(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(reviewService.reviewsForTime(id));
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }
}
